﻿using System.Web.DynamicData;
using System.Web.Compilation;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace Fatec.ControleEstoque.Interface
{
    public partial class DynamicLinkControl : System.Web.UI.UserControl
    {
        public string Text
        {
            get
            {
                if (ViewState["text"] != null)
                {
                    return ViewState["text"].ToString();
                }
                else
                {
                    return null;
                }
            }

            set
            {
                ViewState["text"] = value;
            }
        }

        public string NavigateUrl
        {
            get
            {
                if (ViewState["navigateUrl"] != null)
                {
                    return ViewState["navigateUrl"].ToString();
                }
                else
                {
                    return null;
                }
            }

            set
            {
                ViewState["navigateUrl"] = value;
            }
        }

        public string Action
        {
            get
            {
                if (ViewState["action"] != null)
                {
                    return ViewState["action"].ToString();
                }
                else
                {
                    return null;
                }
            }

            set
            {
                ViewState["action"] = value;
            }
        }

        public string TableName
        {
            get
            {
                if (ViewState["table"] != null)
                {
                    return ViewState["table"].ToString();
                }
                else
                {
                    return null;
                }
            }

            set
            {
                ViewState["table"] = value;
            }
        }

        public string ContextTypeName
        {
            get
            {
                if (ViewState["context"] != null)
                {
                    return ViewState["context"].ToString();
                }
                else
                {
                    return null;
                }
            }

            set
            {
                ViewState["context"] = value;
            }
        }

        public string ImageUrl
        {
            get
            {
                if (ViewState["imageUrl"] != null)
                {
                    return ViewState["imageUrl"].ToString();
                }
                else
                {
                    return null;
                }
            }

            set
            {
                ViewState["imageUrl"] = value;
            }
        }

        private bool _dataBound;

        protected void Page_DataBind(object sender, System.EventArgs e)
        {
            _dataBound = true;
            if (ContextTypeName != null)
            {
                MetaTable tbl = MetaModel.Default.GetTable(TableName, BuildManager.GetType(ContextTypeName, false, true));

                if (tbl != null)
                {
                    if (Action != null)
                    {
                        Link.NavigateUrl = tbl.GetActionPath(Action, Page.GetDataItem());
                    }
                    else
                    {
                        Link.NavigateUrl = tbl.GetActionPath(null, Page.GetDataItem(), NavigateUrl);
                    }
                }
            }
            else
            {
                Link.NavigateUrl = MetaModel.Default.GetActionPath(TableName, Action, Page.GetDataItem());
            }

            if (ImageUrl != null)
            {
                HtmlImage img = new HtmlImage();
                img.Alt = Text;
                img.Src = ImageUrl;

                Link.Controls.Add(img);

            }

            Literal l = new Literal();
            l.Text = Text;
            Link.Controls.Add(l);
        }

        protected void Page_PreRender(object sender, System.EventArgs e)
        {
            if (!_dataBound)
            {
                if (ContextTypeName != null)
                {
                    MetaTable tbl = MetaModel.Default.GetTable(TableName, BuildManager.GetType(ContextTypeName, false, true));

                    if (tbl != null)
                    {
                        if (Action != null)
                        {
                            Link.NavigateUrl = tbl.GetActionPath(Action);
                        }
                        else
                        {
                            Link.NavigateUrl = NavigateUrl;
                        }
                    }
                }
                else
                {
                    Link.NavigateUrl = MetaModel.Default.GetActionPath(TableName, Action, null);
                }

                if (ImageUrl != null)
                {
                    HtmlImage img = new HtmlImage();
                    img.Alt = Text;
                    img.Src = ImageUrl;

                    Link.Controls.Add(img);

                }

                Literal l = new Literal();
                l.Text = Text;
                Link.Controls.Add(l);
            }

        }
    }
}